package com.example.uiclassassignments.tools;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.uiclassassignments.R;

import java.util.ArrayList;

public class ListViewAdapter extends BaseAdapter {
    private ArrayList<ListViewItemData>mListViewData=new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;

    public ListViewAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mListViewData.size();
    }

    @Override
    public Object getItem(int position) {
        return mListViewData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.listview_activity_list_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((ListViewItemData) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(ListViewItemData object, ViewHolder holder) {
        holder.tvOrder.setText(object.order + "");
        holder.tvDate.setText(object.date);
        holder.tvMoney.setText(object.money);
    }

    protected class ViewHolder {
        private TextView tvOrder;
        private TextView tvDate;
        private TextView tvMoney;

        public ViewHolder(View view) {
            tvOrder = (TextView) view.findViewById(R.id.tv_order);
            tvDate = (TextView) view.findViewById(R.id.tv_date);
            tvMoney = (TextView) view.findViewById(R.id.tv_money);
        }
    }
}

